// nums = [2,7,11,15], target = 9

var twoSum = function(nums, target) {
  for (let i = 0; i < nums.length; i++) { // n

    for (let j = i + 1; j < nums.length; j++) { // n
      if (nums[i] + nums[j] == target) {
        return [i, j]
      }
    }

  }
};


var twoSum2 = function(nums, target) {
  // 一层遍历，当遍历到2 的时候，存进一个对象,
  // let obj = {
  //   2: 0,
  //   7: 1,
  //   11: 2
  // }

  const diffs = {}
  for (let i = 0; i < nums.length; i++) {
    if (diffs[target - nums[i]] !== undefined) {
      return [diffs[target - nums[i]], i]
    }
    diffs[nums[i]] = i
  }
}


// O(n^2)

console.log(twoSum2([2,7,11,15], 9));